/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/fraction-to-recurring-decimal
   @Language: C++
   @Datetime: 19-11-27 15:11
   */

class Solution {
public:
	string fractionToDecimal(int numerator, int denominator) {
		bool sign=(1L*numerator*denominator<0L);
		long n=labs((long)numerator), d=labs((long)denominator), i;
		string integer((sign?"-":"")+to_string(n/d)), fraction;
		unordered_map<long,long> dict;
		for(n=(n%d)*10, i=0; n && !dict.count(n); n=(n%d)*10){
			dict[n]=i++;
			fraction.push_back('0'+n/d);
		}
		if(!n) return integer+(fraction.length()?"."+fraction:"");
		return integer+"."+fraction.substr(0,dict[n])+"("+fraction.substr(dict[n])+")";
	}
};

